﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Core.CMS.ServerControls
{
    public partial class PageTools : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && Page is ManagedPage)
            {
                PageInfo info = ((ManagedPage)Page).Info;
                ui_txtTitle.Text = info.Title;
                ui_txtDesc.Text = info.Description;
                ui_txtKeywords.Text = info.Keywords;
            }
        }

        protected void ui_btnSave_Click(object sender, EventArgs e)
        {
            PageInfo info = ((ManagedPage)Page).Info;
            info.Title = ui_txtTitle.Text;
            info.Description = ui_txtDesc.Text;
            info.Keywords = ui_txtKeywords.Text;
            info.Save();
        }

        protected void ui_btnCreatePage_Click(object sender, EventArgs e)
        {
            string basePath = Page.Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath,string.Empty) + Request.ApplicationPath;
            // create the page info this will automatically update the id in the object
            PageInfo info = new PageInfo();
            info.Title = ui_txtNewTitle.Text;
            info.Description = ui_txtNewDesc.Text;
            info.Keywords = ui_txtNewKeywords.Text;
            info.Parent = ((ManagedPage)Page).Info.ID;
            info.Save();
            // then we simply redirect to it :)
            Response.Redirect(basePath + info.ID);
        }
    }
}